<?php
/**
 * @title 72. 编辑距离
 * @author start2004
 */

// ini_set("memory_limit", "128M");

// class Solution {
//
//     function find() {
//
//     }
// }

class Solution {

    /**
     * @param String $word1
     * @param String $word2
     * @return Integer
     */
    function minDistance($word1, $word2) {
        /**
         * @since 2024-01-02 单词长度
         */
        $this->len1 = strlen($word1);
        $this->len2 = strlen($word2);
        $this->word1 = $word1;
        $this->word2 = $word2;

        /**
         * @since 2024-01-02 避免反复遍历
         */
        $this->dp = [];

        /**
         * @since 2024-01-02 双指针
         * @return Integer
         */
        return $this->MinConvert(0, 0);
    }

    function MinConvert($p1, $p2){
        /**
         * @since 2024-01-02 是否命中缓存
         */
        $key = "{$p1}-{$p2}";
        if(isset($this->dp[$key])){
            return $this->dp[$key];
        } else {}

        /**
         * @since 2024-01-02 都在范围内
         */
        if($p1<$this->len1 AND $p2<$this->len2){
            /**
             * @since 2024-01-02 相同，不操作
             */
            if($this->word1[$p1] == $this->word2[$p2]){
                /**
                 * @return
                 */
                return $this->dp[$key] = $this->MinConvert($p1+1, $p2+1);
            } else {
                /**
                 * @since 2024-01-02 替换
                 */
                $replaceMin = 1+$this->MinConvert($p1+1, $p2+1);

                /**
                 * @since 2024-01-02 删除
                 */
                $deleteMin = 1+$this->MinConvert($p1+1, $p2);

                /**
                 * @since 2024-01-02 插入
                 */
                $insertMin = 1+$this->MinConvert($p1, $p2+1);

                /**
                 * @return
                 */
                return $this->dp[$key] = min($replaceMin, $deleteMin, $insertMin);
            }
        } else {
            /**
             * @since 2024-01-02 至少一个到达边界
             */
            if($p1 == $this->len1){
                /**
                 * @since 2024-01-02 p2都尾，全部插入
                 */
                return $this->dp[$key] = $this->len2 - $p2;
            } elseif($p2 == $this->len2){
                /**
                 * @since 2024-01-02 p1到尾，全部删除
                 */
                return $this->dp[$key] = $this->len1 - $p1;
            } else {}
        }
    }
}


/**
 * @url http://127.0.0.1/leetcode/202401/2024.01.02_9.php
 */
$datas = [
    // ["horse", "ros"], // 3
    // ["intention", "execution"], // 5
    // ["sea", "eat"], // 2
    ["dinitrophenylhydrazine", "acetylphenylhydrazine"], //
];

include_once dirname(__DIR__) . DIRECTORY_SEPARATOR ."xhprof.php";
$xhprof = new Xhprof();
foreach ($datas as $data){
    if(count($data) <= 20){
        var_dump($data);
    } else {
        var_dump(count($data));
    }

    $obj = new Solution();
    $result = $obj->minDistance(...$data);
    // $result = $obj->($xhprof->tree($data));
    // $result = $obj->($xhprof->listNode($data));
    // $result = $obj->find(...$data);
    var_dump($result);
    // if(count($result)<=20){
    //     var_dump($result);
    // } else {
    //     var_dump(count($result));
    // }
    echo str_repeat(PHP_EOL, 3);
    echo str_repeat("<br>", 3);
}

// foreach ($datas as $data){
//     $obj = new $data[0][0](...$data[1][0]);
//
//     for ($i=1; $i<count($data[0]); $i++){
//         $func = $data[0][$i];
//         $param = $data[1][$i];
//         $result = $obj->$func(...$param);
//
//         echo $data[0][$i] ."(\"". implode(",", $data[1][$i]) ."\") ";
//             if($result === true){
//                 echo "True";
//             } elseif($result === false) {
//                 echo "False";
//             } elseif($result === null) {
//                 echo "Null";
//             } elseif(is_array($result)) {
//                 var_dump($result);
//             } else {
//                 echo $result;
//             }
//         echo PHP_EOL;
//     }
//
//     echo str_repeat(PHP_EOL, 3);
// }
$xhprof->end();
